﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Runtime.Serialization;

using Core.Localization.Attributes;
using Core.Model.Column;

namespace Core.Model.Hierarchy
{
	[DataContract]
	public class Research : ICloneable
	{
		#region Public fields

		[DataMember]
		[LocalizableCategory("RAE", "Strings", "General")]
		[LocalizableDisplayName("RAE", "Strings", "Name")]
		public string Name { get; set; }

		[DataMember]
		[LocalizableCategory("RAE", "Strings", "General")]
		[LocalizableDisplayName("RAE", "Strings", "Description")]
		public string Description { get; set; }

		[DataMember]
		[LocalizableCategory("RAE", "Strings", "General")]
		[LocalizableDisplayName("RAE", "Strings", "Date")]
		public DateTime Date { get; set; }

		[DataMember]
		[LocalizableCategory("RAE", "Strings", "Miscellaneous")]
		[LocalizableDisplayName("RAE", "Strings", "Custom_information")]
		public ObservableCollection<Tuple<string, string>> CustomInformation { get; set; }

		[DataMember]
		public IEnumerable<ExaminationColumn> Columns { get; set; }

		[DataMember]
		public ObservableCollection<ExpandoObject> Cases { get; set; }
		
		public bool IsDirty;

		#endregion

		#region Private fields

		private int _nextAutogeneratedId;

		#endregion

		#region Public methods

		public static Research CreateResearch() 
		{
			return new Research {
				Name = "New research",
				Description = "Provide world peace",
				Date = DateTime.Now,
				CustomInformation = new ObservableCollection<Tuple<string, string>>(),
				Columns = new List<ExaminationColumn>{new ExaminationColumn("testColumn", ColumnType.Regular)},
				Cases = new ObservableCollection<ExpandoObject>(),
				_nextAutogeneratedId = 1,
				IsDirty = true
			};
		}

		public int GenerateNextId()
		{
			return _nextAutogeneratedId++;
		}

		public override string ToString()
		{
			return String.Format("{0} - {1}", Name, Date);
		}

		public Research Clone()
		{
			return new Research
			{
				Name = Name,
				Description = Description,
				Date = Date,
				CustomInformation = new ObservableCollection<Tuple<string, string>>(CustomInformation),
				Cases = new ObservableCollection<ExpandoObject>(Cases),
				Columns = new List<ExaminationColumn>(Columns),
				_nextAutogeneratedId = _nextAutogeneratedId
			};
		}

		#endregion

		#region ICloneable implementation

		object ICloneable.Clone()
		{
			return Clone();
		}

		#endregion
	}
}